/** @QFi-template-author: wanghua 
 ** @path view/vecspace/middleplatform/filemanagementpart/uploadFileStorage/uploadFileStorage_edit.js  view/vecspace/middleplatform/filemanagementpart/uploadFileStorage/uploadFileStorage_edit.ftl
 ** @url  api/view/middleplatform_uploadFileStorageHandler/edit?id=
 ** @class UploadFileStorageHandler.edit()
 **/

var isAdd=true;
var formData;
$().ready(function() {
	$(".pinned").pin({containerSelector: ".content", minWidth: 100});
	$('input').iCheck(); //$('#xxx').iCheck('check')	$('#xxx').iCheck('uncheck');
	$('input').on('ifChanged', function(event){ //checkbox或者radio输入框的 checked 或 disabled 状态改变了，由iCheck组件触发

	});
	if(idValue && idValue != "null"){
		getData(idValue);
		isAdd = false;
	};
	setValidateRule();
	$("#form1").dkform().init();
	//$('#birthday2').datetimepicker('remove'); //移除时间选择器
	//$("#birthday").datetimepicker({format: 'yyyy-mm-dd',minView:2,pickerPosition:"top-right"}); //指定日期面板的显示位置在输入框的上方

});

function btnSaveClick(){
	$("#form1").submit();
}

//初始化表单数据
function getData(id){
	dkDialog_showLoading();
	var url = basePath + 'api/base/middleplatform_uploadFileStorageHandler/getUploadFileStorage';
	$.post(url,{"id":id},
		function(data, textStatus, jqXHR){
			dkDialog_closeLoading();
			if(data.hasError){
				alert(data.errorMessage); 
			}else{
				var item = data;
				formData = data;
				/**对特殊数据做格式处理**/
				//item.storageSize = numberUtils.numberFormatsToFixed(item.storageSize);
				//item.usableStatus = numberUtils.numberFormatsToFixed(item.usableStatus);
				//item.displayOrdinal = numberUtils.numberFormatsToFixed(item.displayOrdinal);

			$("#form1").json2form({'data':item});
			$('.selectpicker').selectpicker('refresh');
			$('input').iCheck('update');
		}
	}).error(function(){
			dkDialog_closeLoading();
			alert("执行http请求时发生内部错误");
	});
}

$.validator.setDefaults({
	submitHandler: function() { 
		var obj = $("#form1").form2json(formData);
    	obj.id = idValue;
		/**再次编辑或设置提交对象的属性**/
		//obj.date = dateUtils.dateToStamp(obj.date);
		obj.storageSize = numberUtils.toNumber(obj.storageSize);
		obj.usableStatus = numberUtils.toNumber(obj.usableStatus);
		obj.displayOrdinal = numberUtils.toNumber(obj.displayOrdinal);

		var jsonstr = JSON.stringify(obj);
		hideSubmitButton();
		dkDialog_showLoading();
		var url = basePath + 'api/base/middleplatform_uploadFileStorageHandler/saveUploadFileStorage';

		$.post(url, {"item":jsonstr},
			function(data, textStatus, jqXHR){
				dkDialog_closeLoading();
				if(data.hasError){
					showSubmitButton();
					alert(data.errorMessage); 
				}else{
					hideSubmitButton();
					var resultType = isAdd ? 'added' : 'edited';
					dialogframe.closeDialog({"type":resultType, "dataId": data.id});
				}
		}).error(function(){
			dkDialog_closeLoading();
			alert("执行http请求时发生内部错误");
		}); //end post
	}//end submitHandler
});

//验证规则
function setValidateRule(){
	$("#form1").validate({
		rules: { //email:true, url:true,  creditcard :true
			storageName: { /*存储名称*/
				maxlength: 64,
				minlength: 1,
				required:true
		} ,
			storagePath: { /*存储路径*/
				maxlength: 128,
				minlength: 1,
				required:true
		} ,
			storageSize: { /*存储空间大小（MB）*/
				maxlength: 11,
				digits: true,
				minlength: 1,
				required:true
		} ,
			usableStatus: { /*可用状态*/
				maxlength: 6,
				digits: true,
				max: 9999,
				minlength: 1,
				required:true
		} ,
			remarks: { /*备注*/
				maxlength: 128,
				minlength: 0
		} ,
			displayOrdinal: { /*顺序号*/
				maxlength: 6,
				digits: true,
				max: 9999,
				minlength: 1,
				required:true
		} 

    },
		messages: {
			storageName: {
			},
			storagePath: {
			},
			storageSize: {
			},
			usableStatus: {
			},
			remarks: {
			},
			displayOrdinal: {
			}

    }
  });
}
function hideSubmitButton(){
  $("#btnSave").hide();
}

function showSubmitButton(){
  $("#btnSave").show();
}
